//
//  LoginMethodTableViewCell.swift
//  ezyform
//
//  Created by Moennig Q on 19/7/2022.
//  Copyright © 2022 Client.Solution Pty Ltd. All rights reserved.
//

import UIKit

class LoginMethodTableViewCell: UITableViewCell {
    var switchBtnClosure: ((Bool)->Void)?
    lazy var typeImageView: UIImageView = {
        let image = UIImageView()
        return image
    }()
    lazy var typeDescripLabel: BaseLabel = {
        let label = BaseLabel()
        label.textColor =  UIColor(hexStr: "#444444")
        label.setFont(R.font.sfProTextMedium(size: 16))
        return label
    }()
    var switchBtn: SwitchButton!
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.initUI()
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
    }
    override func setSelected(_ selected: Bool, animated: Bool) {
        self.backgroundColor = .clear
    }
    private func initUI(){
        //
        self.contentView.backgroundColor = .white
        self.contentView.addSubview(typeImageView)
        self.typeImageView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(20)
            make.width.height.equalTo(24)
            make.centerY.equalToSuperview()
        }
        //
        self.contentView.addSubview(typeDescripLabel)
        typeDescripLabel.snp.makeConstraints { make in
            make.left.equalTo(typeImageView.snp.right).offset(12)
            make.width.height.greaterThanOrEqualTo(0)
            make.centerY.equalToSuperview()
        }
        //
        switchBtn = SwitchButton()
        self.contentView.addSubview(switchBtn)
        switchBtn.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-20)
            make.size.equalTo(SwitchButton.size)
            make.centerY.equalToSuperview()
        }
        switchBtn.addTarget(self, action: #selector(switchBtnClick(_:)), for: UIControl.Event.touchUpInside)
    }
    @objc func switchBtnClick(_ sender: SwitchButton){
        //SwitchButton自定义内部有switch 的代码，这里直接取值即可
        let onFlag = (sender.tag == 1 ? true : false)
        self.switchBtnClosure?(onFlag)
    }
    func setData(_ model: LoginMethodModel){
        switch model.operType {
        case .gesture:
            self.typeImageView.image = R.image.loginmethod_gesture()//UIImage(named: "loginmethod_gesture")
            self.typeDescripLabel.text = R.string.localizable.loginMethodSettingGesturelogin()
        case .touchID:
            self.typeImageView.image = R.image.loginmethod_touchid()//UIImage(named: "loginmethod_touchid")
            self.typeDescripLabel.text = R.string.localizable.loginMethodSettingTouchidlogin()
        case .faceID:
            self.typeImageView.image = R.image.loginmethod_faceid()//UIImage(named: "loginmethod_faceid")
            self.typeDescripLabel.text = R.string.localizable.loginMethodSettingFaceidlogin()
        default:
            break
        }
        self.switchBtn.switchOn(model.isOn)
    }
}
